package com.woniuxy.rabbitmqspringboot.configuration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author zhangyong
 * @Date 2021/8/12 0012上午 11:31
 */
@Configuration
public class RabbitmqConfiguration {
    /**
     * simple 、work
     */
    @Bean
    public Queue queue(){
        return new Queue("boot_simple_work");
    }

    /**
     * fanout   订阅模式
     */
    //消息队列
    @Bean
    public Queue fanoutQueueA(){
        return new Queue("fanout_A");
    }
    @Bean
    public Queue fanoutQueueB(){
        return new Queue("fanout_B");
    }

    //交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout_exchange");
    }

    //将队列绑定到交换机上
    @Bean
    public Binding bindingFanoutQueueAToFanoutExchange(Queue fanoutQueueA,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutQueueA).to(fanoutExchange);
    }
    @Bean
    public Binding bindingFanoutQueueBToFanoutExchange(Queue fanoutQueueB,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutQueueB).to(fanoutExchange);
    }

    /**
     * direct 路由
     */

    //两个队列
    @Bean
    public Queue directQueueA(){
        return new Queue("direct_A");
    }
    @Bean
    public Queue directQueueB(){
        return new Queue("direct_B");
    }
    //一个交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("direct_exchange");
    }

    //将队列绑定到交换机上
    @Bean
    public Binding bindingDirectQueueAToDirectExchange(Queue directQueueA,DirectExchange directExchange){
        //绑定并且指定路由
        return BindingBuilder.bind(directQueueA).to(directExchange).with("news");
    }
    @Bean
    public Binding bindingDirectQueueBToDirectExchange(Queue directQueueB,DirectExchange directExchange){
        //绑定并且指定路由
        return BindingBuilder.bind(directQueueB).to(directExchange).with("weather");
    }

    /**
     * topic
     */
    //两个队列
    @Bean
    public Queue topicQueueA(){
        return new Queue("topic_A");
    }
    @Bean
    public Queue topicQueueB(){
        return new Queue("topic_B");
    }

    //一个交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topic_exchange");
    }

    //将队列绑定到交换机上
    @Bean
    public Binding bindingTopicQueueAToTopicExchange(Queue topicQueueA,TopicExchange topicExchange){
        //绑定并且指定路由
        return BindingBuilder.bind(topicQueueA).to(topicExchange).with("#.news");
    }
    @Bean
    public Binding bindingTopicQueueBToTopicExchange(Queue topicQueueB,TopicExchange topicExchange){
        //绑定并且指定路由
        return BindingBuilder.bind(topicQueueB).to(topicExchange).with("chengdu.#");
    }

}
